package com.gyx.lottery.controller;

import com.gyx.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.gyx.lottery.common.exception.ControllerException;
import com.gyx.lottery.common.pojo.CommonResult;
import com.gyx.lottery.common.utils.JacksonUtil;
import com.gyx.lottery.controller.param.ShortMessageLoginParam;
import com.gyx.lottery.controller.param.UserPasswordLoginParam;
import com.gyx.lottery.controller.param.UserRegisterParam;
import com.gyx.lottery.controller.result.UserInfoResult;
import com.gyx.lottery.controller.result.UserPasswordLoginResult;
import com.gyx.lottery.controller.result.UserRegisterResult;
import com.gyx.lottery.service.UserService;
import com.gyx.lottery.service.VerificationCodeService;
import com.gyx.lottery.service.dto.UserDTO;
import com.gyx.lottery.service.dto.UserLoginDTO;
import com.gyx.lottery.service.dto.UserRegisterDTO;
import com.gyx.lottery.service.enums.UserIdentifyEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private VerificationCodeService verificationCodeService;
    private static final Logger logger= LoggerFactory.getLogger(UserController.class);
        @RequestMapping("/register")
    public CommonResult<UserRegisterResult>userRegister(@Validated @RequestBody UserRegisterParam param){
            logger.info("userRegister UserRegisterParam:{}",param);
            //打印日志，
            //调用service层方法进行具体业务逻辑实现
            UserRegisterDTO userRegisterDTO=userService.register(param);
            return CommonResult.success(conveterUserRegisterResult(userRegisterDTO));
        }

    public  UserRegisterResult conveterUserRegisterResult(UserRegisterDTO userRegisterDTO) {
            UserRegisterResult userRegisterResult=new UserRegisterResult();
            if(null==userRegisterDTO){
                throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
            }
            userRegisterResult.setUserId(userRegisterDTO.getUserId());
    return userRegisterResult;
    }
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber){
            logger.info("sendVerificationCode 中 phoneNumber为{}",phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);

    }
    @RequestMapping("/password/login")
    public CommonResult<UserPasswordLoginResult> userPasswordLogin
            (@RequestBody @Validated UserPasswordLoginParam param){
        logger.info("userPasswordLogin 中 UserPasswordLoginParam为:{}", JacksonUtil.writeVauleAsString(param));
        UserLoginDTO userLoginDTO=userService.login(param);
        return  CommonResult.success(conveterUserPasswordLoginResult(userLoginDTO));
    }
    @RequestMapping("/message/login")
    public CommonResult<UserPasswordLoginResult> shortMessageLogin
            (@RequestBody @Validated ShortMessageLoginParam param){
        logger.info("shortMessageLogin 中 ShortMessageLoginParam:{}", JacksonUtil.writeVauleAsString(param));
        UserLoginDTO userLoginDTO=userService.login(param);
        return  CommonResult.success(conveterUserPasswordLoginResult(userLoginDTO));
    }

    private UserPasswordLoginResult conveterUserPasswordLoginResult(UserLoginDTO userLoginDTO) {
            if(userLoginDTO==null){
                throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
            }
            UserPasswordLoginResult result=new UserPasswordLoginResult();
            result.setIdentity(userLoginDTO.getIdentiy().name());
            result.setToken(userLoginDTO.getToken());
            return result;
    }
    @RequestMapping("/base-user/find-list")
    public CommonResult<List<UserInfoResult>> findBaseUserInfo(String identity){
            logger.info("findBaseUserInfo 中identity:{}",identity);//打印日志
            List<UserDTO> userListDTOList=userService.findUserList(UserIdentifyEnum.forName(identity));//调用service层方法
           return CommonResult.success(converToUserList(userListDTOList));
    }

    private List<UserInfoResult> converToUserList(List<UserDTO> userListDTOList) {
            return userListDTOList.stream().map(userDTO->{
                UserInfoResult userInfoResult=new UserInfoResult();
                userInfoResult.setUserId(userDTO.getUserId());
                userInfoResult.setIdentity(userDTO.getIdentity().name());
                userInfoResult.setUserName(userDTO.getUserName());
                return  userInfoResult;
            }).collect(Collectors.toList());
    }


}
